#include "sys_inc.h"
#include "sip_uac.h"
#include "sip_uas.h"
#include "SIPMessage.h"
UAContext UContext = { 0 };


const char* enc_sdp = "v=0\r\n"
"o=34020001001320000002 3899 3899 IN IP4 192.168.1.127\r\n"
"s=Play\r\n"
"c=IN IP4 192.168.1.127\r\n"
"t=0 0\r\n"
"m=video 15060 RTP / AVP 96\r\n"
"a=sendonly\r\n"
"a=rtpmap:96 PS / 90000\r\n"
"a=filesize: 0\r\n"
"m=audio 25060 RTP / AVP 96\r\n"
"a=sendonly\r\n"
"a=rtpmap:96 PS / 8000\r\n"
"a=filesize: 0\r\n"
"a=vkek:<2020-07-28T15:34:02><MHoCIQDh0LJvlJI0YFjcKq6HKcXmWEOFVqsn4hES2Ij+WSAdPQIhAJd98tW7+3BCalNKnZ5lv3iomD4FUieecv94OZB0pvmoBCBLjavaab0NxR1h/UZQMOX8geO+uZtBfe+p3K+1H2qO+gQQMmoPEVTK5XZq4xWPImv+xQ ==>\r\n"
"y=0200010002\r\n"
"f=v/2/4/25/1/1024\r\n";

const char* uac_code = "34020000001320000001";
const char* uac_domain = "3402000000";
const char* uac_ip = "192.168.1.8";
uint16_t uac_port = 5060;
const char* uas_code = "34020000002000000001";
const char* uas_domain = "3402000000";
const char* uas_ip = "192.168.1.127";
uint16_t uas_port = 5060;
int expire_time = 3600;
uint16_t CSeq = 0;
uint16_t protocol = SIP_UDP;

void* handle = NULL;

int SIPCallback(void* sip, UAEncrypt* crypt,  int flags,const char* body, int len, void* user)
{

	char *buffer;
	int size = 0x10000;

	UAXTranslateMessage(handle,sip,&buffer, &size);
	printf("%s:%ld\n", buffer,size);
	return 0;
}

int main(int argc, char* argv[])
{
	{
		strcpy(UContext.uac_code, "34020000001320000001");
		strcpy(UContext.uac_domain, "3402000000");
		strcpy(UContext.uac_ip, "192.168.1.8");

		strcpy(UContext.uas_code, "34020000002000000001");
		strcpy(UContext.uas_domain, "3402000000");
		strcpy(UContext.uas_ip, "192.168.1.8");
		strcpy(UContext.password, "12345678");

		UContext.uac_port = 5060;
		UContext.uas_port = 5066;
		UContext.CSeq = 0;
		UContext.protocol = SIP_UDP;
	}

#ifdef WIN32
	WSAData WSData = { 0 };
	WSAStartup(MAKEWORD(2, 2), &WSData);
#endif

	CSIPMessage SIPMessage;
	SIPMessage.OpenChannel(&UContext, 1);
	while (true) {
		Sleep(1);
	}
	return 0;

	handle = UAXCreateHandle(SIPCallback, NULL);
	
	char buffer[0x10000];
	FILE* fp = fopen("sipmessage.txt", "rb");
	int size = fread(buffer, 1, 0x10000, fp);
	buffer[size] = '\0';
	fclose(fp);
	printf("\n%s", buffer);
	UAXParseTcpMessage(handle, buffer, size);

	while (true) {
		Sleep(1);
	}
	{
		sdp_param_t param;

		UAXSdpMessage(&param, enc_sdp);
		printf("%s\n", param.address);
	}

	{
		char *buffer;
		int  size  = 0;
		void* req = UACRegisterReq(&UContext, 3600);
		UAXTranslateMessage(handle,req, &buffer, &size);
		printf("%s\n", buffer);

	}
	{
		char buffer[0x10000];
		size_t size = 0x10000;
		void* req = UACHeartbeatReq(&UContext);
		//UAXTranslateMessage(req, buffer, &size);
		printf("%s\n", buffer);
	}

	{
		char buffer[0x10000];
		size_t size = 0x10000;
		void* req = UASQueryCatalogReq(&UContext);
		//UAXTranslateMessage(req, buffer, &size);
		printf("%s\n", buffer);
	}

	{
		CataDevice item[2] = {0};
		char buffer[0x10000];
		size_t size = 0x10000;
		void* req = UACQueryCatalogRsp(&UContext,item, uac_code, 2, 2,0);
		//UAXTranslateMessage(req, buffer, &size);
		printf("%s\n", buffer);
	}
	//void*  req = UACRegisterReq(uac_code, uac_domain, uac_ip, uac_port, uas_code, uas_domain, uas_ip, uas_port, expire_time, CSeq,protocol);
	//void* req = UACHeartbeatReq(uac_code, uac_domain, uac_ip, uac_port, uas_code, uas_domain, uas_ip, uas_port,CSeq, protocol,60,23456);
	//char* buffer;
	//size_t size = 2048;
	//osip_message_to_str(req,&buffer, &size);
	//printf("%s\n", buffer);

	return 0;
}